package ch.selise.archtool.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.acl.LastOwnerException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JList;

import ch.selise.archtool.lib.product.furniture.FurnitureDAO;
import ch.selise.archtool.lib.product.furniture.FurnitureUnit;
import ch.selise.exercise.pattern.notepad.command.CommandExecutor;
import ch.selise.exercise.pattern.notepad.command.GetCommandParameter;
import ch.selise.exercise.pattern.notepad.exception.EmptyStackException;
import ch.selise.exercise.pattern.notepad.exception.ExecuteException;
import ch.selise.exercise.pattern.notepad.exception.UnsupportedCommandException;
import ch.selise.exercise.pattern.notepad.storage.AppStorageSingleton;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JComboBox;
import javax.swing.JTextArea;

public class FurnitureWindow extends JFrame {

	private JPanel contentPane;
	private JList<FurnitureUnit> furnitureList;
	private JTextArea selectedFurnitureArea;
	
	private List< ? extends FurnitureUnit> furnitures;
	private DefaultListModel model;

	CommandExecutor ce = CommandExecutor.getInstance();
	private JPanel panel;
	private JButton btnUndo;
	private JButton btnRedo;
	
	public List<FurnitureUnit> getSelectedFurnitures(){
		List<FurnitureUnit> selected = new ArrayList<FurnitureUnit>();
		List<String> strSelected = AppStorageSingleton.getInstance().getText();
		
		for (String string : strSelected) {
			for (FurnitureUnit fu : furnitures) {
				if (fu.compareTo(string) == 1){
					selected.add(fu);
				};
			}
		}
		
		return selected;
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame frame = new FurnitureWindow(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FurnitureWindow(Context context) {
		furnitures = FurnitureDAO.getInstance().getFurnitures();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		contentPane.add(createFurnitureSelectionSection(), BorderLayout.WEST);
		
		selectedFurnitureArea = new JTextArea();
		contentPane.add(selectedFurnitureArea, BorderLayout.CENTER);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		
		btnUndo = new JButton("Undo");
		btnUndo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					ce.executeCommand(new GetCommandParameter("undo", "", 0));
				} catch (UnsupportedCommandException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecuteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (EmptyStackException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				paintTextArea();
			}
		});
		panel.add(btnUndo);
		
		btnRedo = new JButton("Redo");
		btnRedo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ce.executeCommand(new GetCommandParameter("redo", "", 0));
					
				} catch (UnsupportedCommandException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ExecuteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (EmptyStackException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				paintTextArea();
			}
		});
		panel.add(btnRedo);
		
		
		
		if (context != null) {
			panel.add(context.getDoneButton());
			panel.add(context.getCancleButton());
			
		}
		
		
		
	}
	
	private JPanel createFurnitureSelectionSection(){
		model = new DefaultListModel<FurnitureUnit>();
	    for(int i=0; i<furnitures.size(); i++){
	      model.addElement(furnitures.get(i));
	    }
	    furnitureList = new JList(model);
	    furnitureList.setVisibleRowCount(4);
	    Font displayFont = new Font("Serif", Font.BOLD, 18);
	    furnitureList.setFont(displayFont);
	    JScrollPane listPane = new JScrollPane(furnitureList);
		
		JPanel listPanel = new JPanel();
	    listPanel.setBackground(Color.white);
	    Border listPanelBorder =
	      BorderFactory.createTitledBorder("Furnitures");
	    listPanel.setBorder(listPanelBorder);
	    listPanel.add(listPane);
		//listPanel.setVisible(true);
	    JButton addButton = new JButton("Add to list");
	    addButton.addActionListener(new ItemAdder());
	    listPanel.add(addButton);
		return listPanel;
		
	}
	private class ItemAdder implements ActionListener {
	    public void actionPerformed(ActionEvent event) {
	    
	    	FurnitureUnit furniture =	furnitureList.getSelectedValue();
	    
	    	if(furniture == null){return;}
	    	
	    	try {
				ce.executeCommand(new GetCommandParameter("add", furniture.toString(),0));
				
				paintTextArea();
				
			} catch (UnsupportedCommandException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecuteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (EmptyStackException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	    	
	    	//selectedFurnitureArea.append(furniture.toString());
	    }
	  }
	
	private void paintTextArea()
	{
		List<String> furnituresText = AppStorageSingleton.getInstance().getText();
		
		selectedFurnitureArea.setText("");
		for (String furnitureText : furnituresText) {
			selectedFurnitureArea.append(furnitureText);
			selectedFurnitureArea.append("\n");
		}
	}
}


